package com.mini.service;

import com.mini.constant.RedisKeyConst;
import com.mini.model.BusinessCompanyInfo;
import com.mini.repository.BusinessCompanyInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangxiaobin
 * @title: BusinessCompanyInfoService
 * @description: TODO
 * @date 2020/2/411:05
 */
@Service
public class BusinessCompanyInfoService extends BaseService {

    @Autowired
    private BusinessCompanyInfoRepository businessCompanyInfoRepository;

    public List<BusinessCompanyInfo> findAll(){
        String allCompanyInfosKey = RedisKeyConst.getCompanyInfoListKey(0);
        Object o = redisTemplate.opsForValue().get(allCompanyInfosKey);
        if(o == null) {
            List<BusinessCompanyInfo> allCompanyInfos = businessCompanyInfoRepository.findAllByState(0);
            if(allCompanyInfos != null && allCompanyInfos.size()>0) {
                redisTemplate.opsForValue().set(allCompanyInfosKey, allCompanyInfos, 60, TimeUnit.DAYS);
            }
            return allCompanyInfos;
        }
        return (List<BusinessCompanyInfo>)o;
    }

    public BusinessCompanyInfo findById(Long companyId){
        String companyInfoKey = RedisKeyConst.getCompanyInfoKey(companyId);
        Object o = redisTemplate.opsForValue().get(companyInfoKey);
        if(o == null) {
            BusinessCompanyInfo one = businessCompanyInfoRepository.findOne(companyId);
            if(one != null){
                redisTemplate.opsForValue().set(companyInfoKey,one,60,TimeUnit.DAYS);
            }
            return one;
        }
        return (BusinessCompanyInfo)o;

    }

}
